package com.example.hospitalsystemclient.ui.home;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.hospitalsystemclient.MainActivity;
import com.example.hospitalsystemclient.databinding.FragmentHomeBinding;
import com.example.hospitalsystemclient.ui.LoginActivity;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("localData", 0);
        String patientIdentify = sharedPreferences.getString("patientIdentify", "");
        String patientName = sharedPreferences.getString("patientName", "");
        int patientId = sharedPreferences.getInt("patientId", -1);
        String patientGender = sharedPreferences.getString("patientGender", "");
        int patientAge = sharedPreferences.getInt("patientAge", -1);
        String patientAddr = sharedPreferences.getString("patientAddr", "");
        String patientPhone = sharedPreferences.getString("patientPhone", "");

        TextView tvPatientIdentify = binding.tvPatientIdentify;
        TextView tvPatientName = binding.tvPatientName;
        TextView tvPatientId = binding.tvPatientId;
        TextView tvPatientGender = binding.tvPatientGender;
        TextView tvPatientAge = binding.tvPatientAge;
        TextView tvPatientAddr = binding.tvPatientAddr;
        TextView tvPatientPhone = binding.tvPatientPhone;

        tvPatientName.setText(patientName);
        tvPatientIdentify.setText("身份证号: " + patientIdentify);
        tvPatientId.setText("账号: " + patientId);
        tvPatientGender.setText("性别: " + patientGender);
        tvPatientAge.setText("年龄: " + patientAge);
        tvPatientAddr.setText("地址: " + patientAddr);
        tvPatientPhone.setText("电话: " + patientPhone);

        Button logoutButton = binding.logout;
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences1 = getActivity().getSharedPreferences("localData", 0);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.apply();
                Toast.makeText(getActivity(), "已登出", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });


        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}